package com.farsunset.webhook.controller.hoxin;

import com.farsunset.webhook.controller.request.QuoteRequest;
import com.farsunset.webhook.feign.ChatGptBotService;
import com.farsunset.webhook.feign.HoxinRobotService;
import com.farsunset.webhook.feign.request.OpenAIRequest;
import com.farsunset.webhook.feign.request.RobotMessageRequest;
import com.farsunset.webhook.service.RobotChatService;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 和信群机器人webhook实现
 */
@RestController
public class RobotController {


	@Resource
	private RobotChatService robotChatService;

	@Resource
	private HoxinRobotService hoxinRobotService;

	@Resource
	private ChatGptBotService chatGptBotService;

	@PostMapping(value = "/robot")
	public ResponseEntity<Void> handle(@RequestBody QuoteRequest request) {

		String text = robotChatService.get(request.getContent(),request.getUuid());

		RobotMessageRequest messageRequest = new RobotMessageRequest();

		messageRequest.setContent(text);

		messageRequest.setMessageId(request.getMessageId());

		hoxinRobotService.send(request.getUuid(),messageRequest);

		return ResponseEntity.ok(null);
	}

	@PostMapping(value = "/chat-gpt")
	public ResponseEntity<Void> gptChat(@RequestBody QuoteRequest request) {

		OpenAIRequest completionRequest = new OpenAIRequest();
		completionRequest.setPrompt(request.getContent());

		String text = chatGptBotService.get(completionRequest ).getText();

		if (StringUtils.hasText(text)) {

			RobotMessageRequest messageRequest = new RobotMessageRequest();

			messageRequest.setContent(text);

			messageRequest.setMessageId(request.getMessageId());

			hoxinRobotService.send(request.getUuid(), messageRequest);
		}

		return ResponseEntity.ok(null);
	}

}
